import router from '../../router/index';
import dynamicRouters from '../../router/dynamicRouters';

const state = {
  routes: []
};

const mutations = {
  setRoutes: (state, routes) => {
    state.routes = routes;
  }
};

const actions = {
  generateRoutes({ commit, state }) {
    // 获取用户路由权限
    // const userRouters = await getUserRouters(state.uid);
    // 格式化权限生成对应的权限树
    // const payload = await formatRouterTree(userRouters);
    // 根据权限树生成菜单路由
    // const newRouters = await generateRouter(payload);
    // console.log('newRouters--', newRouters)
    // 将动态路由和默认路由结合生成新的路由
    // dynamicRouter[0].children = newRouters;
    // router.addRoutes(dynamicRouter);

    return new Promise(resolve => {
      router.addRoutes(dynamicRouters);
      commit('setRoutes', dynamicRouters);
      resolve();
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
